package com.itheima.proxy;

import com.itheima.service.impl.UserServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class UserServiceCglibProxy {
    public static UserServiceImpl createCglibProxy(Class c){
//    创建enhancer对象
   Enhancer enhancer= new Enhancer();
 //设置enhancer对象的父类指定类型时userServiceImpl
        enhancer.setSuperclass(c);
        Callback cb=new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("前置通知");
                Object o1= methodProxy.invokeSuper(o, objects);
                System.out.println("后置通知");
                return o1;
            }
        };
//        设置回调方法
    enhancer.setCallback(cb);
    return (UserServiceImpl)enhancer.create();
    }
}
